package spana;

import lib.common.Util;
import lib.kemi.chem.Chem;

/** Dialog to change logK.
 * <br>
 * Copyright (C) 2014 I.Puigdomenech.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see http://www.gnu.org/licenses/
 * 
 * @author Ignasi Puigdomenech */
public class LogKchange extends javax.swing.JFrame {
    private final java.awt.Dimension windowSize;
    private boolean finished = false;
    private Chem.ChemSystem cs = null;
    private Chem.ChemSystem.NamesEtc namn = null;
    private final int species;
    private boolean deactivated_old;
    private double lBeta_old;
    private boolean modified;

  //<editor-fold defaultstate="collapsed" desc="Constructor">
  /** Creates new form logKchange
   * @param chemS
   * @param complex */
  public LogKchange(Chem.ChemSystem chemS, int complex) {
    initComponents();
    this.cs = chemS;
    this.namn = cs.namn;
    this.species = complex;
    modified = false;
    setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
    //center Window on Screen
    windowSize = this.getSize();
    int left; int top;
    left = Math.max(0, (SpanaFrame.screenSize.width  - windowSize.width ) / 2);
    top = Math.max(0, (SpanaFrame.screenSize.height - windowSize.height) / 2);
    this.setLocation(Math.min(SpanaFrame.screenSize.width-100, left),
                     Math.min(SpanaFrame.screenSize.height-100, top));
    //--- close window on ESC key
    javax.swing.KeyStroke escKeyStroke = javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE,0, false);
    getRootPane().getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(escKeyStroke,"ESCAPE");
    javax.swing.Action escAction = new javax.swing.AbstractAction() {
        @Override public void actionPerformed(java.awt.event.ActionEvent e) {
            quitDialog();
        }};
    getRootPane().getActionMap().put("ESCAPE", escAction);
    //--- Alt-X eXit
    javax.swing.KeyStroke altXKeyStroke = javax.swing.KeyStroke.getKeyStroke(
            java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.ALT_MASK, false);
    getRootPane().getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(altXKeyStroke,"ALT_X");
    javax.swing.Action altXAction = new javax.swing.AbstractAction() {
        @Override public void actionPerformed(java.awt.event.ActionEvent e) {
            jButtonOK.doClick();
        }};
    getRootPane().getActionMap().put("ALT_X", altXAction);
    //--- F1 for help
    javax.swing.KeyStroke f1KeyStroke = javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1,0, false);
    getRootPane().getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(f1KeyStroke,"F1");
    javax.swing.Action f1Action = new javax.swing.AbstractAction() {
        @Override public void actionPerformed(java.awt.event.ActionEvent e) {
            jButtonHelp.doClick();
        }};
    getRootPane().getActionMap().put("F1", f1Action);

    //--- Title
    this.setTitle("Change logK");
    //---- Icon
    String iconName = "images/LogK_32x32.gif";
    java.net.URL imgURL = this.getClass().getResource(iconName);
    if (imgURL != null) {this.setIconImage(new javax.swing.ImageIcon(imgURL).getImage());}
    else {Util.exceptn("Error: Could not load image = \""+iconName+"\"");}

    // --- set up frame
    jLabelReaction.setText(spana.ModifyChemSyst.reactionText(cs, species, false));
    lBeta_old = cs.lBeta[species];
    jLabelLogKOld.setText(Util.formatDbl4(lBeta_old));
    jTextFieldLogK.setText(Util.formatDbl4(lBeta_old));
    setCheckBox(namn.ident[cs.Na + species]);
    deactivated_old = jRadioButtonDeact.isSelected();

    setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
    this.setVisible(true);
  }
  //</editor-fold>

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabelReaction = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldLogK = new javax.swing.JTextField();
        jLabelLogKOld = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jRadioButtonAct = new javax.swing.JRadioButton();
        jRadioButtonDeact = new javax.swing.JRadioButton();
        jPanelButtons = new javax.swing.JPanel();
        jButtonQuit = new javax.swing.JButton();
        jButtonOK = new javax.swing.JButton();
        jButtonHelp = new javax.swing.JButton();
        jLabelWarning = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 0, 204));
        jLabel1.setText("Reaction:"); // NOI18N

        jLabelReaction.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabelReaction.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabelReaction.setText("8 Cu 2+ + 8 H3(urid)  =  Cu8(urid)8 + 24 H+"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelReaction, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelReaction)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(0, 0, 204));
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("Enter new logK:"); // NOI18N

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(0, 0, 204));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("old logK = "); // NOI18N

        jTextFieldLogK.setText("-999.999"); // NOI18N
        jTextFieldLogK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldLogKActionPerformed(evt);
            }
        });
        jTextFieldLogK.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextFieldLogKFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldLogKFocusLost(evt);
            }
        });
        jTextFieldLogK.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextFieldLogKKeyPressed(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextFieldLogKKeyTyped(evt);
            }
        });

        jLabelLogKOld.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabelLogKOld.setText("jLabel5"); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 99, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 33, Short.MAX_VALUE)
        );

        buttonGroup.add(jRadioButtonAct);
        jRadioButtonAct.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jRadioButtonAct.setForeground(new java.awt.Color(0, 0, 204));
        jRadioButtonAct.setMnemonic('a');
        jRadioButtonAct.setText("activate");
        jRadioButtonAct.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonActActionPerformed(evt);
            }
        });

        buttonGroup.add(jRadioButtonDeact);
        jRadioButtonDeact.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jRadioButtonDeact.setForeground(new java.awt.Color(0, 0, 204));
        jRadioButtonDeact.setMnemonic('d');
        jRadioButtonDeact.setText("deactivate");
        jRadioButtonDeact.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonDeactActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(157, 157, 157)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRadioButtonDeact)
                    .addComponent(jRadioButtonAct))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelLogKOld)
                    .addComponent(jTextFieldLogK, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jLabelLogKOld))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jTextFieldLogK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(4, 4, 4)
                .addComponent(jRadioButtonAct)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButtonDeact)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jButtonQuit.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jButtonQuit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/spana/images/Quit_32x32.gif"))); // NOI18N
        jButtonQuit.setMnemonic('Q');
        jButtonQuit.setText("<html><u>Q</u>uit</html>"); // NOI18N
        jButtonQuit.setHorizontalAlignment(javax.swing.SwingConstants.LEADING);
        jButtonQuit.setIconTextGap(8);
        jButtonQuit.setMargin(new java.awt.Insets(2, 2, 2, 2));
        jButtonQuit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonQuitActionPerformed(evt);
            }
        });

        jButtonOK.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jButtonOK.setIcon(new javax.swing.ImageIcon(getClass().getResource("/spana/images/Save_32x32.gif"))); // NOI18N
        jButtonOK.setMnemonic('O');
        jButtonOK.setText("OK"); // NOI18N
        jButtonOK.setHorizontalAlignment(javax.swing.SwingConstants.LEADING);
        jButtonOK.setIconTextGap(8);
        jButtonOK.setMargin(new java.awt.Insets(2, 2, 2, 2));
        jButtonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOKActionPerformed(evt);
            }
        });

        jButtonHelp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/spana/images/Help_32x32.gif"))); // NOI18N
        jButtonHelp.setMnemonic('H');
        jButtonHelp.setMargin(new java.awt.Insets(2, 2, 2, 2));

        javax.swing.GroupLayout jPanelButtonsLayout = new javax.swing.GroupLayout(jPanelButtons);
        jPanelButtons.setLayout(jPanelButtonsLayout);
        jPanelButtonsLayout.setHorizontalGroup(
            jPanelButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelButtonsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButtonHelp)
                    .addGroup(jPanelButtonsLayout.createSequentialGroup()
                        .addComponent(jButtonOK, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10)
                        .addComponent(jButtonQuit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanelButtonsLayout.setVerticalGroup(
            jPanelButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelButtonsLayout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(jPanelButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButtonOK)
                    .addComponent(jButtonQuit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
                .addComponent(jButtonHelp)
                .addContainerGap())
        );

        jLabelWarning.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabelWarning.setIcon(new javax.swing.ImageIcon(getClass().getResource("/spana/images/Warn_RedTriangl.gif"))); // NOI18N
        jLabelWarning.setText("<html>Warning! This species is not active.<br>This may result in wrong diagrams!</html>"); // NOI18N
        jLabelWarning.setAlignmentY(0.0F);
        jLabelWarning.setVerticalTextPosition(javax.swing.SwingConstants.TOP);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelWarning, javax.swing.GroupLayout.PREFERRED_SIZE, 331, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelButtons, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabelWarning))
                    .addComponent(jPanelButtons, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

  //<editor-fold defaultstate="collapsed" desc="Events">
    private void jButtonQuitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonQuitActionPerformed
        quitDialog();
}//GEN-LAST:event_jButtonQuitActionPerformed

    private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOKActionPerformed
        validateLogK();
        lBeta_old = cs.lBeta[species];
        deactivated_old = jRadioButtonDeact.isSelected();
        quitDialog();
}//GEN-LAST:event_jButtonOKActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        quitDialog();
    }//GEN-LAST:event_formWindowClosing

    private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
      if(windowSize != null) {
        int w = Math.round((float)windowSize.getWidth());
        int h = Math.round((float)windowSize.getHeight());
        if(this.getHeight()<h){this.setSize(this.getWidth(), h);}
        if(this.getWidth()<w){this.setSize(w,this.getHeight());}
      }
    }//GEN-LAST:event_formComponentResized

    private void jTextFieldLogKFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldLogKFocusLost
        validateLogK();
    }//GEN-LAST:event_jTextFieldLogKFocusLost

    private void jTextFieldLogKKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldLogKKeyPressed
        if(evt.getKeyChar() == java.awt.event.KeyEvent.VK_ENTER) {
            validateLogK();
        }
    }//GEN-LAST:event_jTextFieldLogKKeyPressed

    private void jTextFieldLogKKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldLogKKeyTyped
        char key = evt.getKeyChar();
        if(!isCharOKforNumberInput(key)) {evt.consume();}
    }//GEN-LAST:event_jTextFieldLogKKeyTyped

    private void jTextFieldLogKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldLogKActionPerformed
        validateLogK();
    }//GEN-LAST:event_jTextFieldLogKActionPerformed

    private void jTextFieldLogKFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldLogKFocusGained
        jTextFieldLogK.selectAll();
    }//GEN-LAST:event_jTextFieldLogKFocusGained

    private void jRadioButtonActActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonActActionPerformed
        if(jRadioButtonAct.isSelected()) {
          if(namn.ident[cs.Na+species].startsWith("*")) {
            namn.ident[cs.Na+species] = namn.ident[cs.Na+species].substring(1);
          }
        } else { // radiobutton not selected
          if(!namn.ident[cs.Na+species].startsWith("*")) {
            namn.ident[cs.Na+species] = "*"+namn.ident[cs.Na+species];
          }
        }
        jLabelReaction.setText(spana.ModifyChemSyst.reactionText(cs, species, false));
        setCheckBox(namn.ident[cs.Na+species]);
    }//GEN-LAST:event_jRadioButtonActActionPerformed

    private void jRadioButtonDeactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonDeactActionPerformed
        if(jRadioButtonDeact.isSelected()) {
          if(!namn.ident[cs.Na+species].startsWith("*")) {
            namn.ident[cs.Na+species] = "*"+namn.ident[cs.Na+species];
          }
        } else { // radiobutton not selected
          if(namn.ident[cs.Na+species].startsWith("*")) {
            namn.ident[cs.Na+species] = namn.ident[cs.Na+species].substring(1);
          }
        }
        jLabelReaction.setText(spana.ModifyChemSyst.reactionText(cs, species, false));
        setCheckBox(namn.ident[cs.Na+species]);
    }//GEN-LAST:event_jRadioButtonDeactActionPerformed

  //</editor-fold>

  //<editor-fold defaultstate="collapsed" desc="Methods">
  private void quitDialog() {
    if(Math.abs(cs.lBeta[species] - lBeta_old) > 1e-8 ||
       deactivated_old != jRadioButtonDeact.isSelected()) {modified = true;}
    if(modified) {
      int n;
      Object[] options = {"Cancel", "Yes"};
          n = javax.swing.JOptionPane.
                  showOptionDialog(this,"Discard your changes?",
                  "Modify Chemical System", javax.swing.JOptionPane.YES_NO_OPTION,
                  javax.swing.JOptionPane.WARNING_MESSAGE, null, options, null);
      if(n != javax.swing.JOptionPane.NO_OPTION) { // second button = "yes"
          return; // oops!
      } else { // yes: discard changes
          cs.lBeta[species] = lBeta_old;
          if(deactivated_old) {
            if(!namn.ident[cs.Na + species].startsWith("*")) {
              namn.ident[cs.Na + species] = "*"+namn.ident[cs.Na + species];
            }
          } else {
            if(namn.ident[cs.Na + species].startsWith("*")) {
              namn.ident[cs.Na + species] = namn.ident[cs.Na + species].substring(1);
            }
          }
      } // discard changes?
    } //if modified
    finished = true;
    this.notify_All();
    this.dispose();
  } // quitForm_Gen_Options

  private synchronized void notify_All() {notifyAll();}

  /** this method will wait for this dialog frame to be closed
   * @return "modified"
   */
  public synchronized boolean  waitForLogKchange() {
    while(!finished) {
        try {wait();} catch (InterruptedException ex) {}
    } // while
    return modified;
  } // waitForLogKchange()

  private void setCheckBox(String name) {
    if(name.startsWith("*")) {
        jRadioButtonDeact.setSelected(true);
        jRadioButtonDeact.setText("deactivated");
        jRadioButtonAct.setText("activate");
        jLabelWarning.setVisible(true);
    } else {
        jRadioButtonAct.setSelected(true);
        jRadioButtonDeact.setText("deactivate");
        jRadioButtonAct.setText("activated");
        jLabelWarning.setVisible(false);
    }
  }

  private void validateLogK(){
    cs.lBeta[species] = readLogK();
    String t = Util.formatNum(cs.lBeta[species]);
    if(Double.parseDouble(t) != cs.lBeta[species]) {
        jTextFieldLogK.setText(t);
    }
  }

  private double readLogK() {
    if(jTextFieldLogK.getText().length() <=0) {return 0;}
    double w;
    try{w = Double.parseDouble(jTextFieldLogK.getText());
        w = Math.min(1e50,Math.max(w, -1e50));
        } //try
    catch (NumberFormatException nfe) {w = Double.NaN;
                System.out.println("Error reading logK:\n"+
                "   "+nfe.toString());
        } //catch
    return w;
  } //readLogK()

  /** @param key a character
   * @return true if the character is ok, that is, it is either a number,
   * or a dot, or a minus sign, or an "E" (such as in "2.5e-6") */
  private boolean isCharOKforNumberInput(char key) {
        return Character.isDigit(key)
                || key == '-' || key == '+' || key == '.' || key == 'E' || key == 'e';
  } // isCharOKforNumberInput(char)

  //</editor-fold>

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup;
    private javax.swing.JButton jButtonHelp;
    private javax.swing.JButton jButtonOK;
    private javax.swing.JButton jButtonQuit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabelLogKOld;
    private javax.swing.JLabel jLabelReaction;
    private javax.swing.JLabel jLabelWarning;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanelButtons;
    private javax.swing.JRadioButton jRadioButtonAct;
    private javax.swing.JRadioButton jRadioButtonDeact;
    private javax.swing.JTextField jTextFieldLogK;
    // End of variables declaration//GEN-END:variables

}
